//
//  TSSharedWebViewContainer.m
//  AppCommonJSCollect
//
//  Created by qian on 2023/10/20.
//  Copyright © 2023 dvlproad. All rights reserved.
//

#import "TSSharedWebViewContainer.h"
#import <WebViewJavascriptBridge/WebViewJavascriptBridge.h>

@interface TSSharedWebViewContainer () {
    
}
@property (nonatomic, strong) WebViewJavascriptBridge *javascriptBridge;


@property (nonatomic, copy) void (^closeMiniProgramHandle)(void);
@property (nonatomic, copy) void (^hideMiniProgramHandle)(void);
@property (nonatomic, copy) void (^openAppUserDetail1Handel)(NSString *userId);
@property (nonatomic, copy) void (^openAppUserDetail2Handel)(NSString *userId);

@end





@implementation TSSharedWebViewContainer

+ (TSSharedWebViewContainer *)sharedInstance {
    static TSSharedWebViewContainer *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[self alloc] init];
    });
    return _sharedInstance;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self addJsBridges];
    }
    return self;
}

- (void)setupJSHandle_closeMiniProgram:(void (^)(void))closeMiniProgramHandle
                       hideMiniProgram:(void (^)(void))hideMiniProgramHandle
              openAppUserDetail1Handel:(void (^)(NSString *userId))openAppUserDetail1Handel
              openAppUserDetail2Handel:(void (^)(NSString *userId))openAppUserDetail2Handel
{
    self.closeMiniProgramHandle = closeMiniProgramHandle;
    self.hideMiniProgramHandle = hideMiniProgramHandle;
    self.openAppUserDetail1Handel = openAppUserDetail1Handel;
    self.openAppUserDetail2Handel = openAppUserDetail2Handel;
}

/**
 *  加载网络网页/本地网页
 *
 *  @param requestUrl   网络网页/本地地址
 */
- (void)reloadUrl:(NSString *)requestUrl {
    if ([requestUrl hasPrefix:@"http"]) {
        //NSString *Url = @"https://baidu.com";
        //NSString *Url = @"http://localhost/h5_native_interacte/h5_mini_program/dvlp_mini_program_demo_JavascriptBridge?title=test&randomNum=000";
        //NSString *Url = @"https://dvlproad.github.io/Architecture%E6%9E%B6%E6%9E%84/h5_native_interacte/h5js/dvlp_h5js_demo.html";
        //NSLog(@"Url = %@", Url);
        [self reloadNetworkWebWithUrl:requestUrl networkEnable:YES];
    } else {
        //NSString *localHtmlUrl = [[NSBundle mainBundle] pathForResource:@"dvlp_mini_program_demo_JavascriptBridge.html" ofType:nil];
        //NSString *localHtmlUrl = [[NSBundle mainBundle] pathForResource:@"dvlp_mini_program_user_page.html" ofType:nil];
        [self reloadLocalWebWithUrl:requestUrl];
    }
}

- (void)addJsBridges {
    [self.javascriptBridge registerHandler:@"commonShare" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"commonShare data = %@", data);
    }];
    
    [self.javascriptBridge registerHandler:@"commonShowShare" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"commonShowShare data = %@", data);
    }];
    
    [self.javascriptBridge registerHandler:@"mpCallApp_closeMiniProgram" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"mpCallApp_closeMiniProgram data = %@", data);
        __weak typeof(self)weakSelf = self;
        if (weakSelf.closeMiniProgramHandle != nil) {
            weakSelf.closeMiniProgramHandle();
        }
    }];
    [self.javascriptBridge registerHandler:@"mpCallApp_hideMiniProgram" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"mpCallApp_closeMiniProgram data = %@", data);
        __weak typeof(self)weakSelf = self;
        if (weakSelf.hideMiniProgramHandle != nil) {
            weakSelf.hideMiniProgramHandle();
        }
    }];
    [self.javascriptBridge registerHandler:@"mpCallApp_openAppUserDetail1" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"mpCallApp_openAppUserDetail1 data = %@", data);
        NSInteger oldUserIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"pageIndex"];
        NSInteger curUserIndex = oldUserIndex + 1;
        [[NSUserDefaults standardUserDefaults] setInteger:curUserIndex forKey:@"pageIndex"];
        NSString *userId = [NSString stringWithFormat:@"用户%ld", curUserIndex];
        __weak typeof(self)weakSelf = self;
        if (weakSelf.openAppUserDetail1Handel != nil) {
            weakSelf.openAppUserDetail1Handel(userId);
        }
    }];
    [self.javascriptBridge registerHandler:@"mpCallApp_openAppUserDetail2" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"mpCallApp_openAppUserDetail2 data = %@", data);
        NSInteger oldUserIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"pageIndex"];
        NSInteger curUserIndex = oldUserIndex + 1;
        [[NSUserDefaults standardUserDefaults] setInteger:curUserIndex forKey:@"pageIndex"];
        NSString *userId = [NSString stringWithFormat:@"用户%ld", curUserIndex];
        __weak typeof(self)weakSelf = self;
        if (weakSelf.openAppUserDetail2Handel != nil) {
            weakSelf.openAppUserDetail2Handel(userId);
        }
    }];
}


#pragma mark - getters & setters
- (WebViewJavascriptBridge *)javascriptBridge {
    if (!_javascriptBridge) {
        _javascriptBridge = [WebViewJavascriptBridge bridgeForWebView:self.webView];
        [_javascriptBridge setWebViewDelegate:self.webView];
    }
    return _javascriptBridge;
}


@end
